package ttsolver.constraints;

import ttsolver.input.Track;
import ttsolver.input.Train;

public class ConsEndTime extends DefaultConstraint {

	private Train train;
	private Track track;
	private int id;

	public ConsEndTime() {
		super(ConstraintType.CONS_END_TIME);
	}

	public ConsEndTime(Train train, Track track, int id) {
		super(ConstraintType.CONS_END_TIME);
		this.train = train;
		this.track = track;
		this.id = id;
	}
	
	public Train getTrain() {
		return train;
	}

	public void setTrain(Train train) {
		this.train = train;
	}

	public Track getTrack() {
		return track;
	}

	public void setTrack(Track track) {
		this.track = track;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime
				* result
				+ ((train == null) ? 0 : train
						.getId().intValue());
		result = prime
				* result
				+ ((track.getNodeOne() == null) ? 0 : track.getNodeOne()
						.getId().intValue());
		result = prime
				* result
				+ ((track.getNodeTwo() == null) ? 0 : track.getNodeTwo()
						.getId().intValue());
		result = prime * result + id;
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;

		ConsEndTime other = (ConsEndTime) obj;

		if (train == null) {
			if (other.train != null)
				return false;
		} else if (!train.equals(other.train)) {
			return false;
		}
		
		if (track.getNodeOne() == null) {
			if (other.track.getNodeOne() != null)
				return false;
		} else if (!track.getNodeOne().equals(other.track.getNodeOne())) {
			return false;
		}
		
		if (track.getNodeTwo() == null) {
			if (other.track.getNodeTwo() != null)
				return false;
		} else if (!track.getNodeTwo().equals(other.track.getNodeTwo())) {
			return false;
		}
		
		if (id != other.id) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		String s = "";
		s += (train != null) ? train.getId() : "N/A";
		s += (track.getNodeOne() != null) ? "," + track.getNodeOne().getId() : "N/A";
		s += (track.getNodeTwo() != null) ? "," + track.getNodeTwo().getId() : "N/A";
		if(id == 1)
			return "Hozizont_Time_1_(" + s + "):\n";
		else
			return "Hozizont_Time_2_(" + s + "):\n";
	}

}
